package com.sds.sweng.swat.miplatform.ui.design.policies;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.FlowLayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformComponent;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformContainer;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformDesignModel;
import com.sds.sweng.swat.miplatform.ui.design.models.area.AreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.models.area.TabAreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.models.commands.CreateCommand;
import com.sds.sweng.swat.miplatform.ui.design.models.commands.ReorderPartCommand;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.sub.TabContentsSubAreaPart;

public class DesignFlowLayoutEditPolicy extends FlowLayoutEditPolicy {

	protected Command getCreateCommand(CreateRequest request) {
		MiplatformComponent newObject = (MiplatformComponent) request.getNewObject();

		if (getHost().getParent() instanceof TabContentsSubAreaPart
				&& newObject instanceof TabAreaTemplate) {
			return null;
		}

		if (!(newObject instanceof AreaTemplate))
			return null;

		AreaTemplate areaTemplate = (AreaTemplate) newObject;

		if (areaTemplate.getWidth() > 0 && ((MiplatformDesignModel) getHost().getModel()).getContentsWidth() != areaTemplate.getWidth())
			return null;

		CreateCommand command = new CreateCommand(
				(MiplatformContainer) getHost().getModel(),
				(MiplatformComponent) newObject,
				getFeedbackIndexFor(request));

		return command;
	}

	@Override
	protected Command createAddCommand(EditPart child, EditPart after) {
		return null;
	}

	protected Command createMoveChildCommand(EditPart child, EditPart after) {
		int oldIndex = getHost().getChildren().indexOf(child);
		int newIndex = -1;

		if (after != null) {
			newIndex = getHost().getChildren().indexOf(after);

			if (oldIndex < newIndex) {
				newIndex -= 1;
			}

			if (oldIndex == newIndex) {
				return null;
			}
		} else {
			if (getHost().getChildren().size() - 1 == oldIndex)
				return null;
		}


		ReorderPartCommand command = new ReorderPartCommand(
				(MiplatformContainer) getHost().getModel(),
				(MiplatformComponent) child.getModel(),
				newIndex);

		return command;
	}

	protected void showLayoutTargetFeedback(Request request) {
		if (request instanceof ChangeBoundsRequest) {
			List editParts = ((ChangeBoundsRequest) request).getEditParts();

			if (editParts.size() == 0) {
				return;
			}
		}

		super.showLayoutTargetFeedback(request);
	}

	protected EditPolicy createChildEditPolicy(EditPart child) {
		return new NonResizableEditPolicy();
	}

}
